#!/usr/bin/env python3

# Copyright (c) 2020-2021 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


from .._const import Version
from .._errors import SettingsError
from .._settings import TargetSettingsBase
from ._spec import Spec


class TargetSettings(TargetSettingsBase):

    def __init__(self):
        super().__init__()

        self.user_name = None

        self.timezone_index = None
        self.keyboard_layout = None

        self.extra_drivers = []

    def check(self, raise_exception=None):
        if not super().check(raise_exception):
            return False

        spec = Spec(self.arch)
        try:
            # check self.version
            if self.version not in [Version.WINDOWS_XP]:
                raise SettingsError("invalid value of version")

            # check self.user_name
            if self.user_name is None:
                raise SettingsError("invalid value of user name")

            # check self.timezone_index
            if self.timezone_index is None or self.timezone_index not in spec.getTimeZoneIndexList():
                raise SettingsError("invalid value of timezone index")

            # check self.keyboard_layout
            if self.keyboard_layout is None or self.keyboard_layout not in spec.getKeyboardLayoutList():
                raise SettingsError("invalid value of keyboard layout")

            # check self.locale
            # if self.locale is None or self.locale not in spec.getLocaleList():
            #     raise SettingsError("invalid value of locale")

        except SettingsError:
            if raise_exception:
                raise
            else:
                return False

        return True
